﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint
{
    public partial class Paint : Form
    {
        private OperationMode mode;
        private Canvas canvas = new Canvas();
        private bool OnWorking = false;

        public Paint()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }   

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutSimplePaint a = new AboutSimplePaint();
            a.ShowDialog();
        }

        private void attributesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Attributes atr = new Attributes();
            atr.CanvasWidth = ViewPort.Width;
            atr.CanvasHeight = ViewPort.Height;
            if (atr.ShowDialog() == DialogResult.OK) //Nhan Ok thi lam
                                                     //Khi bi dong thi Dialog moi tra ve gia tri
            {
                ViewPort.Width = atr.CanvasWidth; 
                ViewPort.Height = atr.CanvasHeight;
            }
        }

        private void btnLine_Click(object sender, EventArgs e)
        {
            foreach (ToolStripButton b in toolbar.Items)
                b.Checked = false;
            btnLine.Checked = true;
            mode = OperationMode.Line;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            foreach (ToolStripButton b in toolbar.Items)
                b.Checked = false;
            btnSelect.Checked = true;
            mode = OperationMode.Select;
        }

        private void btnRectangle_Click(object sender, EventArgs e)
        {
            foreach (ToolStripButton b in toolbar.Items)
                b.Checked = false;
            btnRectangle.Checked = true;
            mode = OperationMode.Rectangle;
        }

        private void btnCircle_Click(object sender, EventArgs e)
        {
            foreach (ToolStripButton b in toolbar.Items)
                b.Checked = false;
            btnCircle.Checked = true;
            mode = OperationMode.Circle;
        }

        private void ViewPort_MouseDown(object sender, MouseEventArgs e)
        {
            switch (mode)
            {
                case OperationMode.Line:
                    Line l = new Line(e.Location, e.Location);
                    canvas.AddShape(l);
                    OnWorking = true;
                    ViewPort.Invalidate();
                    break;
                case OperationMode.Select:
                    canvas.Select(e.Location);
                    ViewPort.Invalidate();
                    break;
                case OperationMode.Rectangle:
                    MyRectangle r = new MyRectangle(e.Location, e.Location, 0, 0);
                    canvas.AddShape(r);
                    OnWorking = true;
                    ViewPort.Invalidate();
                    break;
                case OperationMode.Circle:
                    Circle c = new Circle(e.Location, e.Location, 0, 0);
                    canvas.AddShape(c);
                    OnWorking = true;
                    ViewPort.Invalidate();
                    break;
            }
        }

        private void ViewPort_MouseMove(object sender, MouseEventArgs e)
        {
            if (OnWorking)
            {
                if (mode == OperationMode.Line)
                {
                    Line l = (Line)canvas.TopShape;
                    l.EndPoint = e.Location;
                    ViewPort.Invalidate();
                }
                else
                    if (mode == OperationMode.Rectangle)
                    {
                        MyRectangle r = (MyRectangle)canvas.TopShape;
                        r.EndPoint = e.Location;
                        r.Width = Math.Abs(e.Location.X - r.StartPoint.X);
                        r.Height = Math.Abs(e.Location.Y - r.StartPoint.Y);
                        ViewPort.Invalidate();
                    }
                    else
                        if (mode == OperationMode.Circle)
                        {
                            Circle c = (Circle)canvas.TopShape;
                            c.EndPoint = e.Location;
                            c.Width = Math.Abs(e.Location.X - c.StartPoint.X);
                            c.Height = Math.Abs(e.Location.Y - c.StartPoint.Y);
                            ViewPort.Invalidate();
                        }
            }
        }

        private void ViewPort_MouseUp(object sender, MouseEventArgs e)
        {
            if (OnWorking)
                OnWorking = false;
            ViewPort.Invalidate();
        }

        private void ViewPort_Paint(object sender, PaintEventArgs e)
        {
            canvas.Draw(e.Graphics);
        }

        

    }

    public class MyPanel : Panel
    {
        public MyPanel()
        {
            DoubleBuffered = true;
        }

    }

    public enum OperationMode
    {
        Select, Line, None, Rectangle, Circle
    };
}
